import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Vector;

import javax.naming.NamingException;

import unilearn.domain.*;
import unilearn.facade.*;

/**
 * @author Yin Zhang, 5393558
 * @version 0.1, 07 May 2008, initial creation
 */
public class StaffUserStory1
{
	private static PrintWriter fileOutput = null;
	private static final SimpleDateFormat DATEFORMAT = new SimpleDateFormat(
			"dd/MM/yyyy 'at' HH:mm:ss");
	private static final String SEPARATELINE = "--------------------------------------------------------";

	public StaffUserStory1() throws FileNotFoundException
	{
		fileOutput = new PrintWriter(new FileOutputStream("staffViewSubmissions.txt"));
	}

	public void printSubmission(String staffID) throws SQLException, NamingException,
		IOException
	{
		fileOutput.println("All assignments assigned to " + staffID + ":");

		Vector<Submission> submissions = SubmissionFacade.getSubmissionsByStaff(staffID);

		for (Submission submission : submissions)
		{
			printAssignment(submission);

			String studentID, submittedDate, fileName;
			DataFile assignmentFile = FileFacade.getFileByID(submission.getFileID());
			fileName = assignmentFile.getFileName();
			assignmentFile.setStatus("downloaded");
			studentID = submission.getStudentNumber();
			submittedDate = formatDate(submission.getDateSubmitted());
			fileOutput.print("Student ID: " + studentID + "\t\t\t");
			fileOutput.print(" submitted on " + submittedDate + "\t\t\t");
			fileOutput.print("File: " + fileName + "\t\t\t");
			fileOutput.print("Status: ");

			Assessment assessment = AssessmentFacade.getBySubmissionID(submission.getSubmissionID());

			if (assignmentFile.getStatus() == "downloaded")
			{
				if (assessment != null)
				{
					fileOutput.print("Marked \t\t\t");
					fileOutput.print("Grade " + assessment.getGrade());
				}
				else
				{
					fileOutput.print("Downloaded \t\t\t");
				}
			}
			else
			{
				fileOutput.print("Unmarked \t\t\t");

			}
			fileOutput.println();
			fileOutput.println(SEPARATELINE);
		}
	}

	private void printAssignment(Submission submission) throws SQLException, NamingException
	{
		Assignment assignment = null;
		Subject subject = null;
		String assignmentName, subjectCode, subjectName;

		assignment = AssignmentFacade.getAssignmentBySubmission(submission);
		assignmentName = assignment.getName();

		subject = SubjectFacade.getSubjectByID(assignment.getSubjectID());
		subjectCode = subject.getSubjectCode();
		subjectName = subject.getSubjectName();

		fileOutput.println("Subject: \t\t" + subjectCode + " - " + subjectName);
		fileOutput.println("Assignment: \t" + assignmentName);
	}

	private static String formatDate(Timestamp timestamp)
	{
		if (timestamp == null)
		{
			return null;
		}
		else
		{
			return DATEFORMAT.format((java.util.Date) timestamp);
		}
	}

	public void close()
	{
		try
		{
			fileOutput.close();
		}
		finally
		{
			fileOutput = null;
		}
	}
}